package ch.myDungeons.characters;

import ch.myDungeons.io.LanguagePack;
import ch.myDungeons.game.MyDungeon;


/**
 * Every NPC in the game is an instance of this class
 * @author Grii
 *
 */

public class NPC extends Person {
	private static final long serialVersionUID = 3137763648814208090L;
	
	/** The amount of exp this npc gives the player if he kills it */
	private int expAward = 10;
	
	public NPC (String name, int level, boolean friendly) {
		super(name, level);
		this.setFriendly(friendly);
	}
	
	public void doTurn(){
		super.doTurn();
		
		boolean nextToEnemy = false;
		int enemyDirection = 0;
		
		boolean surroundedByFriends = true;
		
		for (int i = 0; i < 4; i++) {
			if (isFriendly()) {
				if (getMap().checkForEnemy(getMap().getPostitionFromDirection(i, getPosX(), getPosY())[0], getMap().getPostitionFromDirection(i, getPosX(), getPosY())[1])) {
					nextToEnemy = true;
					enemyDirection = i;
				}
				if (!getMap().checkForAlly(getMap().getPostitionFromDirection(i, getPosX(), getPosY())[0], getMap().getPostitionFromDirection(i, getPosX(), getPosY())[1])
						&& !getMap().checkObstacle(getMap().getPostitionFromDirection(i, getPosX(), getPosY())[0], getMap().getPostitionFromDirection(i, getPosX(), getPosY())[1])) {
					surroundedByFriends = false;
				}
			} else {
				if (getMap().checkForAlly(getMap().getPostitionFromDirection(i, getPosX(), getPosY())[0], getMap().getPostitionFromDirection(i, getPosX(), getPosY())[1])) {
					nextToEnemy = true;
					enemyDirection = i;
				}
				if (!getMap().checkForEnemy(getMap().getPostitionFromDirection(i, getPosX(), getPosY())[0], getMap().getPostitionFromDirection(i, getPosX(), getPosY())[1])
						&& !getMap().checkObstacle(getMap().getPostitionFromDirection(i, getPosX(), getPosY())[0], getMap().getPostitionFromDirection(i, getPosX(), getPosY())[1])) {
					surroundedByFriends = false;
				}
			}
			
		}
		
		if (nextToEnemy) {
			attack(getMap().getPostitionFromDirection(enemyDirection, getPosX(), getPosY())[0], getMap().getPostitionFromDirection(enemyDirection, getPosX(), getPosY())[1]);
		} else if(!surroundedByFriends) {
			while (!move(getRandomDirection()));
		} else {
			console.println(getNameWithPrefix() + " " + LanguagePack.get("cant_do_anything"));
		}
	}
	
	/** Attack a target tile. Returns false if failed */
	public boolean attack(int targetX, int targetY){
		boolean result = super.attack(targetX, targetY);
		if (result) {
			Person target = getMap().tiles[targetX][targetY].personOnTheTile;
			if (target.getCurrentHP() <= 0) {
				target.die();
			}
		}
		return result;
	}
	
	/** Switch the team of this NPC */
	public void changeFriendly(){
		if (isFriendly()) {
			console.println(getNameWithPrefix() + " " + LanguagePack.get("is_now_angry_and_turns_evil"));
		} else {
			console.println(getNameWithPrefix() + " " + LanguagePack.get("is_now_friendly"));
		}
		MyDungeon.game.removePerson(this);
		setFriendly(!isFriendly());
		MyDungeon.game.addPerson(this);
	}
	
	public boolean move(int direction){
		return super.move(direction);
	}
	
	/** Returns name, level, maxHP, maxMP, currentHP, currentMP, friendly, naturalWeapon, baseAttackBonus, baseAttackDice, naturalDamageResistance, activeWeaponIndex, activeArmorIndex, items, xpAward, false */
	public String getSave(){
		String result = super.getSave();
		result += "<PERSONINFO>" + getExpAward() + "<PERSONINFO>false";
		return result;
	}

	public int getExpAward() {
		return expAward;
	}

	public void setExpAward(int expAward) {
		this.expAward = expAward;
	}
}
